﻿using famails.App_Code.source.dto;
using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Customer
{
    public partial class CustomerList : System.Web.UI.Page
    {
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        private void LoadData()
        {
            try
            {
                CM_GroupBUS ulBus = new CM_GroupBUS();
                DataTable table = ulBus.GetAll2Order(getUserLogin().UserId);

                ddlGroup.DataTextField = "GroupName";
                ddlGroup.DataValueField = "GroupID";

                DataRow row = table.NewRow();
                row["GroupID"] = -100;
                row["GroupName"] = "Tất cả";
                table.Rows.InsertAt(row, 0);
                ddlGroup.DataSource = table;
                ddlGroup.DataBind();
            }
            catch (Exception ex)
            {

                pnSuccess.Visible = false;
                pnError.Visible = true;
                lblError.Text = " Đã xảy ra lỗi trong quá trình thực hiện. Vui lòng thử lại !<br/>" + ex.Message;
                logs.Error(userLogin.Username + "-Customer-Group - LoadData", ex);
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_CustomerDTO> Search(string name, string phone, string email, int group, string from, string to)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            List<CM_CustomerDTO> result = new List<CM_CustomerDTO>(); 
            try
            {
                //string name = "", phone = "h", email = ""; int group = -1;
                CM_CustomerBUS ctBUS = new CM_CustomerBUS();
                DataTable customerBySelect = ctBUS.Search(email, phone, name, group, userLogin.UserId, from, to);
                CM_CustomerDTO item = new CM_CustomerDTO();
                foreach (DataRow dr in customerBySelect.Rows)
                {
                    item = new CM_CustomerDTO();
                    item.CustomerID = Convert.ToInt32(dr["CustomerID"] + "");
                    item.CustomerName = dr["CustomerName"] + "";
                    item.Email = dr["Email"] + "";
                    item.Phone = dr["Phone"] + "";
                    item.Sex = dr["Sex"] + "";
                    item.Address = dr["Address"] + "";
                    item.Company = dr["Company"] + "";
                    if (dr["BirthDay"] + "" != "")
                        item.BirthDay = Convert.ToDateTime(dr["BirthDay"] + "");
                    item.GroupName = dr["GroupName"] + "" == "" ? "Mặt định" : dr["GroupName"] + "";
                    result.Add(item);
                }
            }
            catch (Exception ex)
            {
                logs.Error("Customer - DeleteEmail", ex);
            }
            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void DeleteEmail(int id)
        {
            try
            {                
                CM_CustomerBUS ctBUS = new CM_CustomerBUS();
                if (!ctBUS.CheckBeforDelete(id))
                    ctBUS.Delete(id);
                else throw new Exception("Khách hàng này đã mua hàng, bạn không thể xóa!");
            }
            catch (Exception ex)
            {
                logs.Error("Customer - DeleteEmail", ex);
                throw ex;
            }
        }
        
    }
}